package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.entitytype;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.EntityTypeLevel;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.TreeIconSkin;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.EntityOperateTypeMapper;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.EntityTypeMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.*;
import com.cqhilink.iot.jobstandardmanagement.facade.service.entitytype.JsmEntityTypePropertyService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.entitytype.JsmEntityTypeService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.file.JsmFileService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.servicenode.JsmServiceNodeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by huangxueqian@cqhilink.com on 2016/11/21.
 * 生产实体类型服务实现类
 */

public class JsmEntityTypeServiceImpl implements JsmEntityTypeService {
    @Autowired
    private EntityTypeMapper entityTypeMapper;
    @Autowired
    private EntityOperateTypeMapper entityOperateTypeMapper;
    @Autowired
    private JsmEntityTypePropertyService entityTypePropertyService;
    @Autowired
    private JsmServiceNodeService serviceNodeService;

    @Autowired
    private JsmFileService fileService;

    /**
     * @param entityType 设备类型
     * @return
     */
    @Override
    public List<Ztree> listTree(EntityType entityType) {
        return offSet(entityTypeMapper.selectEntityTypeList(entityType), entityType);
    }

    /**
     * @param entityType 生产实体类型
     * @param employee 操作人
     * @param uploadFiles 上传文件
     * @param deletedFiles 删除文件
     * @return
     * @throws Exception
     */
    @Override
    public Integer addEntityType(EntityType entityType, Employee employee, String uploadFiles, String deletedFiles) throws Exception {
        Integer resInt = 0;
        String userId = employee.getEmployeeId();
        Date date = new Date();
        entityType.setStatus((short) 0);
        // 设置人员信息
        if (StringUtils.isNotEmpty(entityType.getCreatorId())) {
            fileService.updateFile(uploadFiles, deletedFiles, entityType.getEntityTypeId());
            entityType.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
            entityType.setModifierId(userId);
            resInt = entityTypeMapper.updateByPrimaryKeySelective(entityType);
        } else {
            //ID在跳转页面的时候就已经放在页面上了所以不需要在赋值一次ID了
            fileService.addDelFile(uploadFiles, entityType.getEntityTypeId());
            entityType.setCreatorId(userId);
            entityType.setCreateEtime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
            entityType.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
            entityType.setModifierId(userId);
            resInt = entityTypeMapper.insertSelective(entityType);

        }
        return resInt;
    }

    /**
     * @param entityTypeId 生产实体类型唯一标识
     * @return
     * @throws Exception
     */
    @Override
    public EntityType selectEntityType(String entityTypeId) throws Exception {
        return entityTypeMapper.selectByPrimaryKey(entityTypeId);
    }

    /**
     * @param entityTypeId 生产实体类型唯一标识
     * @return
     * @throws Exception
     */
    @Override
    public Integer delEntityType(String entityTypeId) throws Exception {
        return entityTypeMapper.deleteByPrimaryKey(entityTypeId);
    }

    /**
     * @param entityTypeId 生产实体类型唯一标识
     * @return
     * @throws Exception
     */
    @Override
    public Integer delete(String entityTypeId) throws Exception {
        EntityTypeProperty param = new EntityTypeProperty();
        param.setEntityTypeId(entityTypeId);
        entityTypePropertyService.deleteParentKey(param);
        return this.delEntityType(entityTypeId);
    }

    /**
     * @param entityTypeId 生产实体类型唯一标识
     * @return
     * @throws Exception
     */
    @Override
    public EntityType findEntityTypeById(String entityTypeId) throws Exception {
        return entityTypeMapper.selectByPrimaryKey(entityTypeId);
    }

    /**
     * 生产实体类型特性值树
     * 获取生产实体类型树 和 实体特性值树
     */
    @Override
    public List<Ztree> toFormlistTree() {
        List<Ztree> lists = new ArrayList<>();
        //获取生产生产实体类型树
        List<Ztree> entityTypeList = offSet(entityTypeMapper.selectEntityTypeList(null), null);
        lists.addAll(entityTypeList);
        List<Ztree> operateList = offSetOperateType(entityOperateTypeMapper.toFormEntityOperateType());
        lists.addAll(operateList);
        return lists;
    }

    /**
     * 查询当前实体 或者当前实体的孩子节点
     * @param entityTypeId
     */
    @Override
    public List<Ztree> getEntityTypeList(String entityTypeId) {
        List<EntityType> entityTypes = entityTypeMapper.selectChildRenEntityType(entityTypeId);
        List<Ztree> ztreeList = null;
        try {
            ztreeList = offSetZtree(entityTypes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ztreeList;
    }

    /**
     * 查询所有的实体类型 和 业务节点树 ，
     * 排除实体类型的查询所有的类
     */
    @Override
    public List<Ztree> getAllEntityTypeZtree() {
        // 查询业务体系的实体类型
        List<Ztree> ztreeList = new ArrayList<>();
        ztreeList.addAll(serviceNodeService.getServieNodeZtree());
        EntityType entityType = new EntityType();
        List<EntityType> entityTypes = entityTypeMapper.selectEntityTypeListByEntityType(entityType);
        try {
            List<Ztree> entityZtree = offSetZtree(entityTypes);
            ztreeList.addAll(entityZtree);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 查询所有实体
        return ztreeList;
    }

    /**
     * @param entityTypeList
     * @return 将生产生产实体类型转换成树
     */
    private List<Ztree> offSet(List<EntityType> entityTypeList, EntityType inputEntityType) {
        List<Ztree> list = new ArrayList<>();
        if (entityTypeList != null) {
            for (EntityType entityType : entityTypeList) {
                Ztree ztree = new Ztree();
                ztree.setId(entityType.getEntityTypeId());
                ztree.setName(entityType.getEntityTypeName());
                ztree.setpId(entityType.getParentEntityTypeId());
                ztree.setOrgLevel(entityType.getEntityLevelCode());
                ztree.setIsEntity(1);
                if ("所有设备".equals(entityType.getEntityTypeName()) || (inputEntityType != null && StringUtils.isNotEmpty(inputEntityType.getEntityTypeName()))) {
                    ztree.setOpen(true);
                } else {
                    ztree.setOpen(false);
                }
                ztree = offsetIconSkin(ztree, entityType);
                list.add(ztree);
            }
        }
        return list;
    }


    /**
     * @param entityTypeList
     * @return 将生产生产实体类型转换成树
     */
    public List<Ztree> offSetZtree(List<EntityType> entityTypeList) throws Exception {
        List<Ztree> list = new ArrayList<>();
        if (entityTypeList != null) {
            for (EntityType entityType : entityTypeList) {
                Ztree ztree = new Ztree();
                ztree.setId(entityType.getEntityTypeId());
                ztree.setName(entityType.getEntityTypeName());
                ztree.setpId(entityType.getParentEntityTypeId());
                ztree.setOrgLevel(entityType.getEntityLevelCode());
                ztree = offsetIconSkin(ztree, entityType);
                ztree.setIsEntity(1);
                ztree.setOpen(false);
                list.add(ztree);
            }
        }
        return list;
    }


    /**
     * 转换图标
     *
     * @param ztree
     * @param entityType
     * @return
     */
    private Ztree offsetIconSkin(Ztree ztree, EntityType entityType) {
        if (Integer.parseInt(entityType.getEntityLevelCode()) == EntityTypeLevel.ENTITY_TYPE_LEVEL_NUM1.getCode()) {
            ztree.setIconSkin(TreeIconSkin.ICON_DEVICE_PROCESS_AREA.getDesc());
            ztree.setOpen(false);
        } else if (Integer.parseInt(entityType.getEntityLevelCode()) == EntityTypeLevel.ENTITY_TYPE_LEVEL_NUM2.getCode()) {
            ztree.setIconSkin(TreeIconSkin.ICON_PROCESS_UNIT.getDesc());
            ztree.setOpen(false);
        } else if (Integer.parseInt(entityType.getEntityLevelCode()) == EntityTypeLevel.ENTITY_TYPE_LEVEL_NUM3.getCode()) {
            ztree.setIconSkin(TreeIconSkin.ICON_D_P_DEVICE.getDesc());
        } else if (Integer.parseInt(entityType.getEntityLevelCode()) == EntityTypeLevel.ENTITY_TYPE_LEVEL_NUM4.getCode()) {
            ztree.setIconSkin(TreeIconSkin.ICON_PARTS.getDesc());
        } else if (Integer.parseInt(entityType.getEntityLevelCode()) == EntityTypeLevel.ENTITY_TYPE_LEVEL_NUM0.getCode()) {
            ztree.setIconSkin(TreeIconSkin.ICON_PRODUCTION_UNIT.getDesc());
            ztree.setOpen(false);
        }
        return ztree;
    }

    /**
     * @param entityOperateTypeList
     * @return
     */
    public List<Ztree> offSetOperateType(List<EntityOperateType> entityOperateTypeList) {
        List<Ztree> list = new ArrayList<>();
        if (entityOperateTypeList != null) {
            for (EntityOperateType entityOperateType : entityOperateTypeList) {
                Ztree ztree = new Ztree();
                ztree.setId(entityOperateType.getEntityOperateId());
                ztree.setName(entityOperateType.getEntityOperateName());
                ztree.setpId(entityOperateType.getEntityTypeId());
                ztree.setIsEntity(0);
                ztree.setOpen(false);
                list.add(ztree);
            }
        }
        return list;
    }

    /**
     * @param entityType 模糊搜索
     *  生产实体类型
     */
    @Override
    public List<EntityType> getEntityTypeTable(EntityType entityType) throws Exception {
        return entityTypeMapper.selectTableList(entityType);
    }

    /**
     * @param entityType 设备类型
     * @return
     * @throws Exception
     */
    @Override
    public JSONObject getEntityTypeTableJson(EntityType entityType) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", entityTypeMapper.selectTableCount(entityType));
        jsonObject.put("rows", JSON.toJSON(entityTypeMapper.selectTableList(entityType)));
        return jsonObject;
    }

    /**
     * @param entityType 设备类型
     * @return
     */
    @Override
    public List<EntityType> selectAllList(EntityType entityType) {
        return entityTypeMapper.selectAllList(entityType);
    }
}
